<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/helper/Abstract.php");

class PHP_CMS_View_Helper_Errors extends PHP_CMS_View_Helper_Abstract {

    public function errors($name=null, $bundle=null, $args=null, $localeKey=null)
    {
        $actionErrors = $this->pageContext->request->getAttribute(MVC::ERROR_KEY);

        if(is_null($actionErrors))
            return;

        if(!$actionErrors->isEmpty()) {

            $xhtml = "";

            // Show errors for a specific field.
            if(!is_null($name) && !is_null($actionErrors->get($name))) {

                $errors = $actionErrors->get($name);

                $xhtml .= "<p class=\"errorField\"><strong>";

                foreach($errors as $actionError) {

                    if($actionError->isResource()) {

                        // Retrieve the message string we are looking for
                        $xhtml .= TagUtils::message($this->pageContext, $bundle,
                           $localeKey, $actionError->getKey(), is_null($args) ? $actionError->getValues() : $args);

                    } else {
                        $xhtml .= $actionError->getKey();
                    }
                    $xhtml .= "<br />";
                }
                
                $xhtml .= "</strong></p>";

                return $xhtml;

            // Show all errors
            } else if(is_null($name)) {

                $errors = $actionErrors->get();

                $xhtml .= "<div id=\"errorMsg\">";
                $xhtml .= "<h3>".$this->message("error.header", _Core::BUNDLE)."</h3>";
                $xhtml .= "<ol>";

                while(list($key, $value) = each($errors)) {
                    foreach($value as $actionError) {
                        if($actionError->isResource()) {

                            // Retrieve the message string we are looking for
                            $message = TagUtils::message($this->pageContext, $bundle,
                                $localeKey, $actionError->getKey(), is_null($args) ? $actionError->getValues() : $args);

                        } else {
                            $message = $actionError->getKey();
                        }

                        if($key == ActionMessage::GLOBAL_MESSAGE) {
                            $xhtml .= "<li>".$message."</li>";
                        } else {
                            $xhtml .= "<li><a href=\"javascript:try { ito(document.getElementById('".htmlentities(str_replace("->", "_", $key))."')) } catch(e) {}\" onclick=\"try { document.getElementById('".htmlentities(str_replace("->", "_", $key))."').focus(); } catch(e) {}\" title=\"".$this->message("error.jumpto")."\">".$message."</a></li>";
                        }
                    }
                }

                $xhtml .= "</ol>";
                $xhtml .= "</div>";

            }

            return $xhtml;

        }

    }

}
?>